<template>
	<body>
		<div id='loginForm'>
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
				<el-form-item label="账号" prop="name">
					<el-input v-model="ruleForm.name"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="ruleForm.password"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
					<el-button type="primary" @click="registerForm('ruleForm')">注册</el-button>
					<el-button @click="resetForm('ruleForm')">重置</el-button>
				</el-form-item>
			</el-form>
		</div>
	</body>
</template>
<script>

	export default {
		data() {
			return {
				ruleForm: {
					name: '',
					password: ''
				},
				rules: {
					name: [{
							required: true,
							message: '请输入账号',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '请输入密码',
							trigger: 'blur'
						}
					],
				}
			};
		},
		methods: {
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.checkLogin();
					} else {
						return false;
					}
				});
			},

			registerForm() {
				alert(this.ruleForm.name + "正在注册")
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},

			checkLogin() {
				this.$http({
					url: '/user/login',
					method: 'post',
					data: this.$qs.stringify({
						username:this.ruleForm.name,
						password:this.ruleForm.password
					},null)
				}).then(res=>{
					if(res.data.code===0){
						alert("登录成功");
						this.$router.replace("/home")
					}else {
						alert("登录失败:"+res.data.msg)
					}
				})
			}
		}
	}
</script>

<style scoped="scoped">
	#loginForm {
		width: 400px;
		margin: auto;
		padding-top: 200px;
	}

	body {
		background-color: antiquewhite;
		height: 700px;
		overflow: hidden;
		margin: 0;
		border: 0;
	}
</style>
